package developer.算法.图论.实现Trie;

/**
 * TrieSolution（发音类似 "try"）或者说 前缀树 是一种树形数据结构，用于高效地存储和检索字符串数据集中的键。
 * 这一数据结构有相当多的应用情景，例如自动补全和拼写检查。
 * <p>
 * 请你实现 TrieSolution 类：
 * <p>
 * TrieSolution() 初始化前缀树对象。
 * void insert(String word) 向前缀树中插入字符串 word 。
 * boolean search(String word) 如果字符串 word 在前缀树中，返回 true（即，在检索之前已经插入）；否则，返回 false 。
 * boolean startsWith(String prefix) 如果之前已经插入的字符串 word 的前缀之一为 prefix ，返回 true ；否则，返回 false 。
 * <p>
 * <p>
 * 输入
 * ["TrieSolution", "insert", "search", "search", "startsWith", "insert", "search"]
 * [[], ["apple"], ["apple"], ["app"], ["app"], ["app"], ["app"]]
 * 输出
 * [null, null, true, false, true, null, true]
 * <p>
 * 解释
 * TrieSolution trie = new TrieSolution();
 * trie.insert("apple");
 * trie.search("apple");   // 返回 True
 * trie.search("app");     // 返回 False
 * trie.startsWith("app"); // 返回 True
 * trie.insert("app");
 * trie.search("app");     // 返回 True
 * <p>
 * <p>
 * 提示：
 * <p>
 * 1 <= word.length, prefix.length <= 2000
 * word 和 prefix 仅由小写英文字母组成
 * insert、search 和 startsWith 调用次数 总计 不超过 3 * 104 次
 */
public class TrieSolution {
    public static void main(String[] args) {
        Trie3 trie = new Trie3();
        trie.insert("apple");
        System.out.println(trie.search("apple"));
        System.out.println(trie.search("app"));
        System.out.println(trie.startsWith("app"));
        trie.insert("app");
        System.out.println(trie.search("app"));
    }


    static class Trie3 {
        Trie3[] children;
        boolean isEnd = false;

        public Trie3() {
            children = new Trie3[26];
        }

        public void insert(String word) {
            Trie3 node = this;
            for (char c : word.toCharArray()) {
                if (node.children[c - 'a'] == null) {
                    node.children[c - 'a'] = new Trie3();
                }
                node = node.children[c - 'a'];
            }
            node.isEnd = true;
        }

        public boolean startsWith(String prefix) {
            return prefixSearch(prefix) != null;
        }

        public boolean search(String word) {
            Trie3 trie3 = prefixSearch(word);
            return trie3 != null && trie3.isEnd;
        }

        private Trie3 prefixSearch(String word) {
            Trie3 node = this;
            for (char c : word.toCharArray()) {
                if (node.children[c - 'a'] != null) {
                    node = node.children[c - 'a'];
                } else {
                    return null;
                }
            }
            return node;
        }
    }


    static class Trie {
        Trie[] children;
        boolean isEnd;

        public Trie() {
            children = new Trie[26];
            isEnd = false;
        }

        public void insert(String word) {
            if (word == null) {
                return;
            }

            Trie node = this;
            for (int i = 0; i < word.length(); i++) {
                char c = word.charAt(i);
                int indx = c - 'a';
                if (node.children[indx] == null) {
                    node.children[indx] = new Trie();
                }
                node = node.children[indx];
            }
            node.isEnd = true;
        }

        public boolean search(String word) {
            Trie trie = prefixSearch(word);
            return trie != null && trie.isEnd;
        }

        public boolean startsWith(String prefix) {
            return prefixSearch(prefix) != null;
        }

        public Trie prefixSearch(String prefix) {
            Trie node = this;
            for (int i = 0; i < prefix.length(); i++) {
                Trie child = node.children[prefix.charAt(i) - 'a'];
                if (child == null) {
                    return null;
                }
                node = child;
            }
            return node;
        }
    }


    class TrieOfficial {
        private TrieOfficial[] children;
        private boolean isEnd;

        public TrieOfficial() {
            children = new TrieOfficial[26];
            isEnd = false;
        }

        public void insert(String word) {
            TrieOfficial node = this;
            for (int i = 0; i < word.length(); i++) {
                char ch = word.charAt(i);
                int index = ch - 'a';
                if (node.children[index] == null) {
                    node.children[index] = new TrieOfficial();
                }
                node = node.children[index];
            }
            node.isEnd = true;
        }

        public boolean search(String word) {
            TrieOfficial node = searchPrefix(word);
            return node != null && node.isEnd;
        }

        public boolean startsWith(String prefix) {
            return searchPrefix(prefix) != null;
        }

        private TrieOfficial searchPrefix(String prefix) {
            TrieOfficial node = this;
            for (int i = 0; i < prefix.length(); i++) {
                char ch = prefix.charAt(i);
                int index = ch - 'a';
                if (node.children[index] == null) {
                    return null;
                }
                node = node.children[index];
            }
            return node;
        }
    }

/**
 * Your TrieSolution object will be instantiated and called as such:
 * TrieSolution obj = new TrieSolution();
 * obj.insert(word);
 * boolean param_2 = obj.search(word);
 * boolean param_3 = obj.startsWith(prefix);
 */
}
